package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Componente;


public class ComponenteDAO implements IComponenteDAO {
    private static final Log log = LogFactory.getLog(ComponenteDAO.class);

    // property constants
    //public static final String  COMPCIADESCRIPT = "compciadescript";
    public static final String COMPCIADESCRIPT = "compciadescript";

    //public static final Integer  COMPCIAIDN = "compciaidn";
    public static final String COMPCIAIDN = "compciaidn";

    //public static final String  COMPCIASIGLAV = "compciasiglav";
    public static final String COMPCIASIGLAV = "compciasiglav";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Componente Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Componente instance) {
        log.debug("saving Componente instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Componente Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Componente instance) {
        log.debug("deleting Componente instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Componente Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Componente instance) {
        log.debug("updating Componente instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Componente findById(Integer id) {
        log.debug("finding Componente instance with id: " + id);

        try {
            Componente instance = (Componente) getSession()
                                                   .get(Componente.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Componente failed", re);
            throw re;
        }
    }

    public List<Componente> findByExample(Componente instance) {
        log.debug("finding Componente instance by example");

        try {
            List results = getSession().createCriteria("Componente")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Componente entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Componente> found by query
        */
    public List<Componente> findByProperty(String propertyName, Object value) {
        log.debug("finding Componente instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Componente as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Componente> findByCompciadescript(Object compciadescript) {
        return findByProperty(COMPCIADESCRIPT, compciadescript);
    }

    public List<Componente> findByCompciaidn(Object compciaidn) {
        return findByProperty(COMPCIAIDN, compciaidn);
    }

    public List<Componente> findByCompciasiglav(Object compciasiglav) {
        return findByProperty(COMPCIASIGLAV, compciasiglav);
    }

    /**
    * Find all Componente entities.
    *
    * @return List<Componente> all Componente instances
    */
    public List<Componente> findAll() {
        log.debug("finding all Componente instances");

        try {
            String queryString = "from Componente";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Componente> findByCriteria(String whereCondition) {
        log.debug("finding Componente " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Componente model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Componente> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Componente failed", re);
            throw re;
        }
    }

    public List<Componente> findPageComponente(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Componente findPageComponente");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Componente model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Componente model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberComponente() {
        log.debug("finding Componente count");

        try {
            String queryString = "select count(*) from Componente model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
